﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AntiAddictionAides
{
    public partial class AppForm : Form
    {
        #region 函数
        public AppForm()
        {
            InitializeComponent();

            #region 初始化
            Inst = this;

            var headers = new string[] { "序号", "进程信息", "防沉迷时间(秒)", "声音提示", "弹框提示", "窗口抖动" };
            foreach(var v in headers)
            {
                this.listViewProcess.Columns.Add(v).TextAlign = HorizontalAlignment.Center;
            }
            this.listViewProcess.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            this.listViewProcess.Columns[1].Width = 150;
            this.listViewProcess.Columns[3].Width = 150;
            this.listViewProcess.Columns[4].Width = 400;
            this.listViewProcess.Columns[5].Width = 96;

            // 读取配置项
            _processMonitor = new ProcessMonitor();
            var configItems = LoadProcessConfig();
            foreach(var v in configItems)
            {
                var lv = this.listViewProcess.Items.Add($"{this.listViewProcess.Items.Count + 1}");
                lv.SubItems.AddRange(new string[] {
                    v.ProcessNamePattern,
                    v.AddictionTime.ToString(),
                    v.SoundAlert,
                    v.MessageBoxText,
                    (v.ShowMessageBox ? "是" : "否")
                });
                var monitor = _processMonitor.SetMonitor(v.ProcessNamePattern, TimeSpan.FromSeconds(v.AddictionTime));
                if(!string.IsNullOrWhiteSpace(v.SoundAlert))
                {
                    monitor.AddAlert(new SoundAlertStrategy(v.SoundAlert));
                }
                if (!string.IsNullOrWhiteSpace(v.MessageBoxText))
                {
                    monitor.AddAlert(new MessageBoxAlertStrategy(v.MessageBoxText));
                }
                if(v.ShowMessageBox)
                {
                    monitor.AddAlert(new WindowShakeStrategy(this.Handle));
                }
            }
            #endregion
        }

        public static void Log(string info)
        {
            lock(Inst)
            {
                Console.WriteLine($"[{DateTime.Now}]{info}");
            }
        }
        #endregion

        #region 内部函数
        static List<ProcessItem> LoadProcessConfig()
        {
            var config = new List<ProcessItem>();
            try
            {
                var section = ConfigurationManager.GetSection("ProcessMonitor") as ProcessMonitorSection;
                foreach (ProcessItemElement element in section.ProcessItems)
                {
                    config.Add(new ProcessItem
                    {
                        ProcessNamePattern = element.ProcessNamePattern,
                        AddictionTime = element.AddictionTime,
                        SoundAlert = element.SoundAlert,
                        MessageBoxText = element.MessageBoxText,
                        ShowMessageBox = element.ShowMessageBox
                    });
                }
            }
            catch (Exception ex)
            {
                AppForm.Log($"配置加载失败: {ex.Message}");
            }
            return config;
        }

        class ProcessItem
        {
            public string ProcessNamePattern { get; set; }
            public int AddictionTime { get; set; }
            public string SoundAlert { get; set; }
            public string MessageBoxText { get; set; }
            public bool ShowMessageBox { get; set; }
        }
        #endregion

        #region 属性变量
        ProcessMonitor _processMonitor;
        public static AppForm Inst { get; set; }
        #endregion

        #region 事件
        private void buttonStart_Click(object sender, EventArgs e)
        {
            if(buttonStart.Text == "启动")
            {
                _processMonitor.Start();
                buttonStart.Text = "停止";
            }
            else
            {
                _processMonitor.Stop();
                buttonStart.Text = "启动";
            }
        }
        #endregion
    }

    #region 配置
    public class ProcessMonitorSection : ConfigurationSection
    {
        [ConfigurationProperty("ProcessItems")]
        public ProcessItemCollection ProcessItems =>
            (ProcessItemCollection)this["ProcessItems"];
    }

    public class ProcessItemCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement() =>
            new ProcessItemElement();

        protected override object GetElementKey(ConfigurationElement element) =>
            ((ProcessItemElement)element).ProcessNamePattern;
    }

    public class ProcessItemElement : ConfigurationElement
    {
        [ConfigurationProperty("processNamePattern", IsRequired = true)]
        public string ProcessNamePattern =>
            (string)this["processNamePattern"];

        [ConfigurationProperty("addictionTime", IsRequired = true)]
        public int AddictionTime =>
            (int)this["addictionTime"];

        [ConfigurationProperty("soundAlert", IsRequired = false)]
        public string SoundAlert =>
            (string)this["soundAlert"];

        [ConfigurationProperty("messageBoxText", IsRequired = false)]
        public string MessageBoxText =>
            (string)this["messageBoxText"];

        [ConfigurationProperty("showMessageBox", IsRequired = true)]
        public bool ShowMessageBox =>
            (bool)this["showMessageBox"];
    }
    #endregion
}
